package com.xuesong.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;

/**
 * @ClassName: R
 * @Description:
 * @author: Gong_Xuesong
 * @date 2021/12/7 17:56
 */
public class R {
    private static final long serialVersionUID = -5001822887685035489L;

    // 200 OK
    private static final String MESSAGE_OK = "操作成功！";

    // 400 BAD_REQUEST
    private static final String MESSAGE_BAD_REQUEST = "参数缺失，请检查参数！";

    // 401 UNAUTHORIZED
    private static final String MESSAGE_UNAUTHORIZED = "帐户已过期，请重新登录！";

    // 403 FORBIDDEN
    private static final String MESSAGE_FORBIDDEN = "无操作权限！";

    // 404 NOT_FOUND
    private static final String MESSAGE_NOT_FOUND = "路径不存在，请检查路径是否正确！";

    // 409 CONFLICT
    private static final String MESSAGE_CONFLICT = "数据已存在！";

    // 500 INTERNAL_SERVER_ERROR
    private static final String MESSAGE_INTERNAL_SERVER_ERROR = "操作失败，请联系管理员！";

    /**
     * 返回代码
     */
    private Integer code;

    /**
     * 返回处理消息
     */
    private String message;

    /**
     * 返回数据对象 data
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Object data;

    /**
     * 构造
     */
    private R() {

    }

    public R(@NonNull HttpStatus code, String message) {
        this(code, message, null);
    }

    public R(@NonNull HttpStatus code, String message, Object data) {
        this.code = code.value();
        this.message = message;
        this.data = data;
    }

    /**
     * 200 OK
     */
    public static R ok() {
        return R.ok(MESSAGE_OK);
    }

    public static R ok(String message) {
        return R.ok(message, null);
    }

    public static R ok(Object data) {
        return R.ok(MESSAGE_OK, data);
    }

    public static R ok(String message, Object data) {
        return R.httpStatusOk(message, data);
    }

    private static R httpStatusOk(String message, Object data) {
        return new R(HttpStatus.OK, message, data);
    }

    /**
     * 400 BAD_REQUEST
     */
    public static R badRequest() {
        return R.badRequest(MESSAGE_BAD_REQUEST);
    }

    public static R badRequest(String message) {
        return R.httpStatusBadRequest(message);
    }

    private static R httpStatusBadRequest(String message) {
        return new R(HttpStatus.BAD_REQUEST, message, null);
    }

    /**
     * 401 UNAUTHORIZED
     */
    public static R MESSAGE_UNAUTHORIZEDMESSAGE_UNAUTHORIZED() {
        return R.unauthorized(MESSAGE_UNAUTHORIZED);
    }

    public static R unauthorized(String message) {
        return R.httpStatusUnauthorized(message);
    }

    private static R httpStatusUnauthorized(String message) {
        return new R(HttpStatus.UNAUTHORIZED, message, null);
    }

    /**
     * 403 FORBIDDEN
     */
    public static R forbidden() {
        return R.forbidden(MESSAGE_FORBIDDEN);
    }

    public static R forbidden(String message) {
        return R.httpStatusForbidden(message);
    }

    private static R httpStatusForbidden(String message) {
        return new R(HttpStatus.FORBIDDEN, message, null);
    }

    /**
     * 404 NOT_FOUND
     */
    public static R notFound() {
        return R.notFound(MESSAGE_NOT_FOUND);
    }

    public static R notFound(String message) {
        return R.httpStatusNotFound(message);
    }

    private static R httpStatusNotFound(String message) {
        return new R(HttpStatus.NOT_FOUND, message, null);
    }

    /**
     * 409 CONFLICT
     */
    public static R conflict() {
        return R.conflict(MESSAGE_CONFLICT);
    }

    public static R conflict(String message) {
        return R.httpStatusConflict(message);
    }

    private static R httpStatusConflict(String message) {
        return new R(HttpStatus.CONFLICT, message, null);
    }

    /**
     * 500 INTERNAL_SERVER_ERROR
     */
    public static R internalServerError() {
        return R.internalServerError(MESSAGE_INTERNAL_SERVER_ERROR);
    }

    public static R internalServerError(String message) {
        return R.httpStatusInternalServerError(message);
    }

    private static R httpStatusInternalServerError(String message) {
        return new R(HttpStatus.INTERNAL_SERVER_ERROR, message, null);
    }
}
